
import java.io.IOException;
import java.util.ArrayList;

import org.apache.commons.math3.stat.correlation.*;


public class correlationCalc {
	private ArrayList<Double>rawData;
	private ArrayList<Double>NASDAQRaw;
	private ArrayList<Double>againstNASDAQ = new ArrayList<Double>();
	private double[]passingRaw;
	private double[]passingStock;
	int smallerStock=0;
	int startCorRange=0;
	int endCorRange=0;
	int[]stockPeriod = {5,15,30,60,90,120};
	PearsonsCorrelation calcAgainstNASDAQ = new PearsonsCorrelation();
	String featureList[] = { "NASDAQIndex", "DJIAIndex", "Nikkei225",
			"S&P500Index", "DAXIndex", "FTSEIndex", "CACIndex", "SHANGHAIIndex", "HANGIndex"  };
	
	
	public correlationCalc(ReadingDataFromCSV temp) throws IOException{
		NASDAQRaw=temp.getStockData().get(0);
		System.out.println("Pearson's Correlation for 6 month period");
		//System.out.print("Total days for correlation: ");
		//Scanner output = new Scanner(System.in);
		//String input = output.next();
		//int daysPrior = Integer.parseInt(input);
		//for(int k=0;k<6;k++){
		//	int daysPrior=stockPeriod[k];
		for(int i=1; i<9;i++){
			
			rawData=temp.getStockData().get(i);
			smallerStock=Math.min(NASDAQRaw.size(),rawData.size());
			passingRaw= new double[smallerStock];
			passingStock= new double[smallerStock];
			endCorRange=smallerStock;
			endCorRange=113;
			//startCorRange=endCorRange-daysPrior;
			
			for(int a=1;a<endCorRange;a++){
				passingRaw[a]=NASDAQRaw.get(a);
			}
			for(int b=1;b<endCorRange;b++){
				passingStock[b]=rawData.get(b);
			}
			System.out.println(featureList[i] + " - " + calcAgainstNASDAQ.correlation(passingRaw,passingStock));
			againstNASDAQ.add(calcAgainstNASDAQ.correlation(passingRaw,passingStock));
			
		}
		System.out.println();
		/* Correlation Ranking
			Pearson's Correlation for 6 month period
			
			CACIndex - 		0.9937777059334357 (6/21)
			DAXIndex - 		0.9937337359977758 (5/21)
			FTSEIndex - 	0.9921671013120049 (4/21)
			HANGIndex - 	0.972354488226973  (3/21)
			SHANGHAIIndex - 0.9593331931813617 (2/21)
			Nikkei225 - 	0.8918565917977268 (1/21)
			
			FTSEIndex - 	0.9946960000319018
			DAXIndex - 		0.9944678854468906
			CACIndex - 		0.9939982639430148
			HANGIndex - 	0.9764874119120132
			SHANGHAIIndex - 0.9721212401767222
			Nikkei225 - 	0.9673208119607064
			
			
			
			
						
		
			Nikkei225 - 	0.9385070762013142 (1/21)
			
			DAXIndex - 		0.9917950100377083 (5/21)
			FTSEIndex - 	0.991976321715882  (6/21)
			CACIndex - 		0.9914110230968578 (4/21)
			SHANGHAIIndex - 0.9576799687256258 (2/21)
			HANGIndex - 	0.9642461016226398 (3/21)

			
			
			
			S&P500Index - 0.9975220515910822 
			DJIAIndex - 0.994790901542669
		 */
		
		
		
		//}	

		/*BufferedReader br = null;

		String stockCorrelationOutput = "src/correlationStocks.csv";
		String csvFile = "";


		FileWriter stockCor = new FileWriter(stockCorrelationOutput);
		PrintWriter stockCorWriter = new PrintWriter(stockCor);

		for (int d = 0; d < againstNASDAQ.size(); d++) {
			stockCorWriter.print(featureList[d + 1]);
			stockCorWriter.print(",");
			stockCorWriter.println(againstNASDAQ.get(d));

		}
			stockCorWriter.flush();
			stockCorWriter.close();
			stockCor.close();
*/
		}
	
		
	}

